package com.csu.filter;

import com.csu.model.Account;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.apache.ibatis.plugin.Intercepts;
import org.springframework.web.servlet.HandlerInterceptor;

public class AccountLoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            HttpSession session = request.getSession();
            //统一拦截
            Account account = (Account) session.getAttribute("loginAccount");
            if(account != null){
                //已登录
                return true;
            }
            //未登录
            response.sendRedirect("/loginForm");
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
